import {I18Parse,CommonLang,Util} from '@lowcode/jpaas-common-lib';
import utilLang from '../locales/lang/impl/utils/utilLang';
import DialogView from "@/layouts/DialogView";
let lang = utilLang;
let i18n=function (langKey,text) {
    return I18Parse.getI18Val(lang.utilLang.msgTips,langKey, text)
}


export function timeFix () {
  const time = new Date()
  const hour = time.getHours()
  return hour < 9 ? i18n('morning','早上好') : hour <= 11 ? i18n('morning','上午好') : hour <= 13 ?  i18n('afternoon','中午好')  : hour < 20 ?  i18n('goodAfternoon','下午好'):  i18n('evening','晚上好')
}

/**
 * 触发 window.resize
 */
export function triggerWindowResizeEvent () {
  const event = document.createEvent('HTMLEvents')
  event.initEvent('resize', true, true)
  event.eventType = 'message'
  window.dispatchEvent(event)
}

export function handleScrollHeader (callback) {
  let timer = 0

  let beforeScrollTop = window.pageYOffset
  callback = callback || function () {}
  window.addEventListener(
    'scroll',
    event => {
      clearTimeout(timer)
      timer = setTimeout(() => {
        let direction = 'up'
        const afterScrollTop = window.pageYOffset
        const delta = afterScrollTop - beforeScrollTop
        if (delta === 0) {
          return false
        }
        direction = delta > 0 ? 'down' : 'up'
        callback(direction)
        beforeScrollTop = afterScrollTop
      }, 50)
    },
    false
  )
}

/**
 * Remove loading animate
 * @param id parent element id or class
 * @param timeout
 */
export function removeLoadingAnimate (id = '', timeout = 1500) {
  if (id === '') {
    return
  }
  setTimeout(() => {
    document.body.removeChild(document.getElementById(id))
  }, timeout)
}

/**
 * 数组项互换
 * @param arr
 * @param index1
 * @param index2
 * @returns {*}
 */
export function swapArrItems (arr, index1, index2) {
  if(index1<0 || index1 > arr.length-1  || index2 <0 || index2 > arr.length-1){
    return arr;
  }
  arr[index1] = arr.splice(index2, 1, arr[index1])[0];
  return arr;
}

/**
 * @description 打开窗口对象.
 * @typedef {Object} openUrlConfig
 * @property {Object} curVm - 当前Vue实例对象.
 * @property {String} title - 标题.
 * @property {String} url - 需要打开的URL.
 * @property {Array} widthHeight - 窗口的高和宽,写法为['600px','400px'].
 * @property {Boolean} max - 是否最大化打开窗口.
 */

/**
 * 打开Iframe窗口.
 * @param {{widthHeight: string[], title, url: string, curVm: default.methods}} config - 配置数据 {@link openUrlConfig}.
 * @example
 * openUrl({curVm,url,title,widthHeight:['600px','400px']},max:true});
 */
export function openUrl(config){
    let {curVm,url,title,widthHeight,max,dialogBox} = config;
    let conf = {
        component: DialogView,
        curVm: curVm,
        title: title || i18n('openWindow','打开弹窗'),
        data: {
            dialogBox:dialogBox,
            url:url
        }
    };
    if(widthHeight){
        conf.widthHeight=widthHeight;
        conf.max=false
    }
    else if(max){
        conf.max=true;
    }
    if(!config.destroy){
        config.destroy=(action,data)=>{};
    }
    Util.open(conf,config.destroy);
}






